package com.sojpt.boot.controller.business;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;


import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sojpt.boot.controller.business.validator.LkyStudentAddValidator;
import com.sojpt.boot.controller.business.validator.LkyStudentUpdateValidator;
import com.sojpt.boot.intercepter.admin.AdminLoginInterceptor;
import com.sojpt.boot.intercepter.admin.AdminPopedomInterceptor;
import com.sojpt.boot.ui.iview.IViewController;
import com.sojpt.kit.SoJLogKit;
import com.sojpt.model.DicPopedom;
import com.sojpt.model.DicUser;
import com.sojpt.model.LkyMajor;
import com.sojpt.model.LkyStudent;
import com.sojpt.util.ResultUtil;
import org.springframework.web.bind.annotation.*;

/**
 * @author Sohnny
 */
@RestController
@RequestMapping("/admin/student/")

public class LkyStudentAdminController extends IViewController {

    //学生信息
    @GetMapping("list")
    public void list() {
        LkyStudent lkyStudent = getModel(LkyStudent.class, "lkyStudent");
        String uid=getPara("uid");
        String roid=Db.queryStr("SELECT `ROLE_ID` FROM `dic_user_role` WHERE `USER_ID`=?",uid);
        SqlPara sqlPara=null;
        Map<String,String> map =new HashMap<>();
        map.put("uid",getPara("uid"));
        map.put("cid",getPara("cid"));
        map.put("mid",getPara("mid"));
        map.put("coid",getPara("coid"));
        map.put("USER_NAME",lkyStudent.getUserName());
        if (roid.equals("a6a1564fdea64acc975266566dc61607")) {   //学院权限
             sqlPara = Db.getSqlPara("lky_student.selectStudentbyCollage", map);
        }else if (roid.equals("7371f2ced4154b8aaab16cccf378e7c3")) {     //老师权限
            sqlPara = Db.getSqlPara("lky_student.selectClassByTid", map);
        }else if(roid.equals("b40d665874274e399985083485a42785"))  {      //总管理
            sqlPara = Db.getSqlPara("lky_student.selectAll", map);
        }

        System.out.println("map***************"+map.entrySet());

        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }

    //通过老师登录的时候传来的id    查询没有激活的学生 需要字段 学生的姓名 学号 学院 专业 班级 激活状态 注册时间（老师）
    @GetMapping("StudentActivateAll")
    public void StudentActivateAll() {
        Map<String,String> map =new HashMap<>();
        map.put("mid",getPara("mid"));
        map.put("coid",getPara("coid"));
        map.put("uid",getPara("uid"));
        System.out.println("map***************"+map.entrySet());
        LkyStudent lkyStudent = getModel(LkyStudent.class, "lkyStudent");
        SqlPara sqlPara = Db.getSqlPara("lky_student.selectStudentActivate",map);
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }
    // 学生查看老师发布任务
    @GetMapping("queryTeacherPublishTask")
    public void queryStudentFillTableRecord() {
        String uid = getPara("uid");
        SqlPara sqlPara = Db.getSqlPara("lky_student.queryTeacherPublishTask", Kv.by("uid", uid));
        List<Record> records = Db.find(sqlPara);
        renderJson(ResultUtil.success(records));
    }

    //查询那个学生填表状态
    @PostMapping("fill")
    public void fill(String id) {
        System.err.println(id);
        String uid=Db.queryStr("SELECT du.`ID`FROM `dic_user` du,`lky_student` ls WHERE du.`USER_CODE` = ls.`student_number`AND ls.`id` = ?",id);
        SqlPara sqlPara = Db.getSqlPara("lky_student.selectFill", Kv.by("uid", uid));
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }

    @PostMapping("saveInfo")
    public void saveInfo(){
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/student/saveInfo");
        LkyStudent lkyStudent=getModel(LkyStudent.class,"student");
        lkyStudent.setId(UUID.randomUUID().toString().replace("-",""));
        //系统当前时间
        Timestamp d = new Timestamp(System.currentTimeMillis());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime=df.format(d.getTime());
        Timestamp timestamp=Timestamp.valueOf(createTime);
        lkyStudent.setCreationTime(timestamp);
        if(lkyStudent.save()){
            dicPopedom.setPopedomName("保存学生信息成功");
            renderJson(Ret.ok());
        }else{
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("保存学生信息失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("update")
    @Before(LkyStudentUpdateValidator.class)
    public void update() {
        LkyStudent lkyStudent = getModel(LkyStudent.class, "lkyStudent");
        if (lkyStudent.update()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    @PostMapping("updateStatus")
    public void updateStatus() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/student/updateStatus");
        String ID=getPara("ID");
        int count = Db.update("UPDATE dic_user SET act_status=1 WHERE ID=?",ID);
        int count1 =Db.update("UPDATE dic_user_role SET ROLE_ID='0ab70a8554f54f4eb98b119cdabdb9c2' WHERE USER_ID=?",ID);
        System.err.println(count);
        if (count > 0 && count1>0) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("学生激活成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("学生激活失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("delete")
    public void delete() {
        LkyStudent lkyStudent = getModel(LkyStudent.class, "lkyStudent");

        if (LkyStudent.dao.deleteById(lkyStudent.getId())) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    @PostMapping("batchDelete")
    public void batchDelete() {
        String[] ids = getPara("ids").split(",");
        SqlPara sqlPara = Db.getSqlPara("lky_student.deleteByIds", Kv.by("idList", Arrays.asList(ids)));
        if (Db.update(sqlPara) > 0) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    /**
     * 登陆拉取本人信息
     */
    @PostMapping("userInfoLogind")
    public void userInfoLogind(){
        DicUser dicUser=getModel(DicUser.class,"user");
        String ID=dicUser.getId();
        SqlPara sqlPara = Db.getSqlPara("admin_user.selectById",ID);
        List<DicUser> dicUsers = DicUser.dao.find(sqlPara);
        renderJson(ResultUtil.success(dicUsers));
    }

    @PostMapping("userInfoLogindByActivated")
    public void userInfoLogindByActivated(){
        String student_number=getPara("student_number");
        Map<String,String> map =new HashMap<>();
        map.put("student_number",student_number);
        SqlPara sqlPara = Db.getSqlPara("admin_user.selectInfoByStudent_number",map);
        Page<Record> page = Db.paginate(1, 10, sqlPara);
        renderJson(page);
    }

    @PostMapping("userInfoVerifyRepetition")
    public void userInfoVerifyRepetition(){
        String student_number=getPara("student_number");
        String id=Db.queryStr("select id from lky_student where student_number=?",student_number);
        if(id==null){
            id="1";
        }
        renderJson(id);
    }


}